package com.jsgt.greekfire.ui.main.activation.fragment

import android.app.Activity.RESULT_OK
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.ActivationBean
import com.jsgt.greekfire.common.util.RecycleEmptyView
import com.jsgt.greekfire.factory.WalletViewModelFactory
import com.jsgt.greekfire.viewmodel.WalletViewModel1
import kotlinx.android.synthetic.main.fragment_activation.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class ActivationFragment : BaseFragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    var onSuccess: (() -> Unit)? = null

    private val walletViewModel1: WalletViewModel1 by lazy {
        ViewModelProvider(
            this,
            WalletViewModelFactory()
        ).get(WalletViewModel1::class.java)
    }

    private val listAdapter: ActivationListAdapter by lazy {
        ActivationListAdapter(R.layout.item_activation_list, null)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_activation
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ActivationFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun main() {
        recycler.layoutManager = LinearLayoutManager(context)
        recycler.adapter = listAdapter
        listAdapter.addChildClickViewIds(R.id.txvActivation)
        listAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                showProgress(true)
                walletViewModel1.activation(listAdapter.getItem(position).guid!!)
            }
        })

        walletViewModel1.activationList()
        walletViewModel1.activationListData.observe(viewLifecycleOwner, Observer {
            if (it.isEmpty()) {
                RecycleEmptyView.showEmptyView(
                    activity as AppCompatActivity?,
                    listAdapter,
                    "暂无地址",
                    R.mipmap.icon_empty_no_data
                )
            } else {
                listAdapter.setNewInstance(it as MutableList<ActivationBean>?)
            }
        })
        walletViewModel1.activationData.observe(viewLifecycleOwner, Observer {
            showProgress(false)
            showToast(getString(R.string.cloud_activation_success))
//            onSuccess?.invoke()
            activity?.setResult(RESULT_OK)
            activity?.finish()
        })
        walletViewModel1.errorLiveData.observe(viewLifecycleOwner, Observer {
            showToast(it.toString())
            showProgress(false)
        })
    }

    class ActivationListAdapter(layoutResId: Int, data: MutableList<ActivationBean>?) :
        BaseQuickAdapter<ActivationBean, BaseViewHolder>(layoutResId, data) {
        override fun convert(holder: BaseViewHolder, item: ActivationBean) {
            holder.setText(R.id.txvName, item.fromUserName)
            holder.setText(R.id.txvAddress, item.address)
            holder.setText(R.id.txvTime, item.createTime)
        }
    }
}